package com.lnn.springBase.controller;

import com.lnn.springBase.common.Result;
import com.lnn.springBase.common.interfaces.IgnoreRestFulAPI;
import com.lnn.springBase.model.UserDTO;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

@RestController
@RequestMapping("/demo")
public class DemoController {
    @GetMapping("/method1")
    public Result<Integer> method1() {
        return Result.success(100);
    }

    @GetMapping("/method2")
    public void method2() {

    }

    @GetMapping(value = "/method3")
    @IgnoreRestFulAPI
    public String method3() {
        return "不会被封装，直接返回 String";
    }

    /**
     * RestController中返回值类型是String的方法默认响应类型是text/plain，需要手动指定为application/json方可对其进行包装
     */
    @GetMapping(value = "/method4", produces = MediaType.APPLICATION_JSON_VALUE)
    public String method4() {
        return "会被封装 Result 结构 JSON";
    }

    /**
     * 会被封装，但是响应类型是text/plain
     * @return
     */
    @GetMapping(value = "/method5")
    public String method5() {
        return "会被封装为 Result 的 text/html";
    }

    @GetMapping("/{num}")
    public Integer detail(@PathVariable("num") @Min(1) @Max(20) Integer num) {
        return num * num;
    }

    @GetMapping("/getByEmail")
    public UserDTO getByAccount(@RequestParam @NotBlank @Email String email) {
        UserDTO testDTO = new UserDTO();
        testDTO.setEmail(email);
        return testDTO;
    }

    @PostMapping("/test-validation")
    public  void  testValidation(@RequestBody @Validated UserDTO testDTO)  {
        //TODO
    }
}
